/// 媒体附件类型枚举
enum MediaType {
  /// 图片
  image,
  /// 音频
  audio,
}

/// 媒体附件模型类
/// 用于存储与情绪记录相关的媒体文件信息
class MediaAttachment {
  /// 附件ID
  final int? id;
  
  /// 关联的情绪记录ID
  final int moodRecordId;
  
  /// 媒体类型
  final MediaType type;
  
  /// 文件路径
  final String filePath;
  
  /// 文件名
  final String fileName;
  
  /// 文件大小（字节）
  final int fileSize;
  
  /// 创建时间
  final DateTime createdAt;
  
  /// 缩略图路径（仅图片类型）
  final String? thumbnailPath;
  
  /// 音频时长（秒，仅音频类型）
  final int? duration;

  /// 构造函数
  const MediaAttachment({
    this.id,
    required this.moodRecordId,
    required this.type,
    required this.filePath,
    required this.fileName,
    required this.fileSize,
    required this.createdAt,
    this.thumbnailPath,
    this.duration,
  });

  /// 从Map创建MediaAttachment对象
  /// [map] 包含媒体附件数据的Map
  factory MediaAttachment.fromMap(Map<String, dynamic> map) {
    return MediaAttachment(
      id: map['id'] as int?,
      moodRecordId: map['mood_record_id'] as int,
      type: MediaType.values[map['type'] as int],
      filePath: map['file_path'] as String,
      fileName: map['file_name'] as String,
      fileSize: map['file_size'] as int,
      createdAt: DateTime.parse(map['created_at'] as String),
      thumbnailPath: map['thumbnail_path'] as String?,
      duration: map['duration'] as int?,
    );
  }

  /// 转换为Map对象，用于数据库存储
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'mood_record_id': moodRecordId,
      'type': type.index,
      'file_path': filePath,
      'file_name': fileName,
      'file_size': fileSize,
      'created_at': createdAt.toIso8601String(),
      'thumbnail_path': thumbnailPath,
      'duration': duration,
    };
  }

  /// 转换为JSON对象，用于数据导出
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'moodRecordId': moodRecordId,
      'type': type.name,
      'filePath': filePath,
      'fileName': fileName,
      'fileSize': fileSize,
      'createdAt': createdAt.toIso8601String(),
      'thumbnailPath': thumbnailPath,
      'duration': duration,
    };
  }

  /// 创建副本，可选择性地更新某些字段
  MediaAttachment copyWith({
    int? id,
    int? moodRecordId,
    MediaType? type,
    String? filePath,
    String? fileName,
    int? fileSize,
    DateTime? createdAt,
    String? thumbnailPath,
    int? duration,
  }) {
    return MediaAttachment(
      id: id ?? this.id,
      moodRecordId: moodRecordId ?? this.moodRecordId,
      type: type ?? this.type,
      filePath: filePath ?? this.filePath,
      fileName: fileName ?? this.fileName,
      fileSize: fileSize ?? this.fileSize,
      createdAt: createdAt ?? this.createdAt,
      thumbnailPath: thumbnailPath ?? this.thumbnailPath,
      duration: duration ?? this.duration,
    );
  }

  /// 判断是否为图片类型
  bool get isImage => type == MediaType.image;

  /// 判断是否为音频类型
  bool get isAudio => type == MediaType.audio;

  /// 获取格式化的文件大小字符串
  String get formattedFileSize {
    if (fileSize < 1024) {
      return '${fileSize}B';
    } else if (fileSize < 1024 * 1024) {
      return '${(fileSize / 1024).toStringAsFixed(1)}KB';
    } else {
      return '${(fileSize / (1024 * 1024)).toStringAsFixed(1)}MB';
    }
  }

  /// 获取格式化的音频时长字符串
  String get formattedDuration {
    if (duration == null) return '';
    final minutes = duration! ~/ 60;
    final seconds = duration! % 60;
    return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
  }

  @override
  String toString() {
    return 'MediaAttachment(id: $id, moodRecordId: $moodRecordId, type: $type, fileName: $fileName)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is MediaAttachment &&
        other.id == id &&
        other.moodRecordId == moodRecordId &&
        other.type == type &&
        other.filePath == filePath;
  }

  @override
  int get hashCode {
    return id.hashCode ^
        moodRecordId.hashCode ^
        type.hashCode ^
        filePath.hashCode;
  }
}